<template>
  <div>
    <el-dialog
      title="授权管理"
      :visible.sync="dialogVisible"
      width="700px"
      :close-on-click-modal="false"
      @close="close"
    >
      <div v-loading="loading" class="dialog-container">
        <el-form ref="form" label-width="120px" size="small">
          <el-form-item label="授权状态：">
            <span v-if="!detail">未授权</span>
            <span v-else-if="detail.base.state === 1">已授权</span>
            <span v-else-if="detail.base.state === 0">授权异常</span>
          </el-form-item>
          <div v-if="detail">
            <el-form-item label="小程序名称：">{{ detail.info.nickName }}</el-form-item>
            <el-form-item label="appId：">{{ detail.base.appId }}</el-form-item>
            <el-form-item label="已授予权限：">{{ detail.permsStr }}</el-form-item>
            <el-form-item v-if="detail.base.state === 0&&detail.base.remark" label="授权异常：">
              <span style="color:#F56C6C">{{ detail.base.remark }}</span>
            </el-form-item>
            <el-form-item v-if="detail.missingPermsStr" label="缺少权限：">
              <span style="color:#F56C6C">{{ detail.missingPermsStr }}</span>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :disabled="loading" round @click="hide">取 消</el-button>
        <el-button
          size="small"
          round
          type="primary"
          :disabled="loading"
          @click="getAuthUrl(2)"
        >{{ detail?'重新授权':'申请授权' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWechatAuthUrl } from '@/api/wechat'
export default {
  data() {
    return {
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      detail: '',
      classifyCode: ''
    }
  },
  methods: {
    open({ auth, classifyCode }) {
      this.detail = auth
      this.classifyCode = classifyCode
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },
    show() {
      this.dialogVisible = true
    },
    close() {
      this.loading = false
      this.detail = null
      this.classifyCode = null
      this.resolve = null
      this.reject = null
    },

    hide() {
      this.dialogVisible = false
    },

    /**
       *  按钮组
       */
    // 申请授权
    getAuthUrl(authType) {
      const cb = location.protocol + '//' + location.host + '/#/wxapp/manage'
      // location.pathname +
      // location.hash;
      const classifyCode = this.classifyCode
      this.loading = true
      getWechatAuthUrl({ authType, cb, classifyCode })
        .then(res => {
          window.location.href = res.data.url
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .dialog-container ::v-deep .el-form-item {
    margin-bottom: 0;
  }
</style>
